<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/22
 */

use App\Services\InfoCategoryService;
use Illuminate\Support\Str;

if (!function_exists('arrayReplaceRecursive')) {
    /**
     * 索引数组递归替换
     * @param array $array
     * @param array $repalce // 例：['password'=>'******']
     * @return array
     */
    function arrayReplaceRecursive($array = [], $repalce = [])
    {
        if (empty($array) || empty($repalce)) return [];
        foreach ($array as $key => $value) {
            foreach ($repalce as $rKey => $rValue) {
                if (is_array($value)) {
                    $array[$key] = arrayReplaceRecursive($value, $repalce);
                } else {
                    if ($key == $rKey) $array[$key] = $rValue;
                }
            }
        }
        return $array;
    }
}

if (!function_exists('getCategory')) {
    /**
     * 根据栏目ID获取栏目详情
     * @param int $category_id
     * @return array|mixed
     */
    function getCategory($category_id = 0)
    {
        if (!$category_id) return [];
        return InfoCategoryService::getDetail($category_id);
    }
}

if (!function_exists('getSubCategoryTree')) {
    /**
     * 获取子栏目树
     * @param int $parent_id 父栏目ID
     * @return array|mixed
     */
    function getSubCategoryTree($parent_id = 0)
    {
        if (!$parent_id) return [];
        return InfoCategoryService::getAllSubsTreeByParentId($parent_id);
    }
}

if (!function_exists('strLimit')) {
    /**
     * 截取字符串
     * @param $value // 字符串
     * @param int $limit // 限制字数，中文15，填15*2
     * @param string $end // 省略部分内容 默认空
     * @return string
     */
    function strLimit($value = '', $limit = 30, $end = ''): string
    {
        return Str::limit($value, $limit, $end);
    }
}

if (!function_exists('getActiveCateIds')) {
    /**
     * 根据指定栏目ID获取导航焦点的栏目ID集合
     * @param int $cate_id 栏目ID
     * @return array
     */
    function getActiveCateIds($cate_id = 0): array
    {
        return array_merge([$cate_id], InfoCategoryService::getParentIdsById($cate_id));
    }
}



